<template>
  <section class="flow-edit-box" v-loading.fullscreen="launching">
    <div class="fd-nav">
      <div class="fd-nav-left">
        <div class="back" @click="back()"><icon-left :size="16" /></div>
        <div class="name-desc" v-if="flow.flowDefine">
          <div class="name">{{ flow.flowName }}</div>
          <div class="desc">{{ flow.remark }}</div>
        </div>
      </div>
      <div class="fd-nav-mid">
        <div class="steps">
          <a-steps changeable :current="step" @change="hanldeStepClick">
            <a-step>基础信息 <template #icon>1</template></a-step>
            <a-step>表单设计 <template #icon>2</template></a-step>
            <a-step>流程设计 <template #icon>3</template></a-step>
            <a-step>更多设置 <template #icon>4</template></a-step>
          </a-steps>
        </div>
      </div>
      <div class="fd-nav-right">

        <a-button @click="deploy()" :disabled="launching" v-if="isEdit">更 新</a-button>
        <a-button @click="deploy()" :disabled="launching" v-else>发 布</a-button>

      </div>
    </div>

    <div class="flow-edit-box">
      <div class="fd-main">
        <transition name="fade-transform" mode="out-in">
          <template v-if="step == 1" :key="1">
            <Base ref="baseBox">
            </Base>
          </template>
          <template v-else-if="step == 2" :key="2">
            <FormMake></FormMake>
          </template>
          <template v-else-if="step == 3" :key="3">
            <div class="fd-main-box" v-dragscroll>
              <Flow ref="flowNodeRef"></Flow>
            </div>
          </template>
          <template v-else-if="step == 4" :key="4">
            <Setting></Setting>
          </template>
        </transition>
      </div>
    </div>
  </section>
</template>

<script setup>
import useApp from "@/hooks";
const { $ } = useApp();
import { useFlowStore } from "@/stores/index";
import { useRouter } from "vue-router";
import { WIDGET } from "@/components/flow/common/FlowConstant";
import { Notification } from "@arco-design/web-vue";
import { IconLeft } from "@arco-design/web-vue/es/icon";
import FlowValidate from "./flow-validate";
import Base from "./base.vue";
import FormMake from "@/components/form-make/index.vue";
import Flow from "@/components/flow/index.vue";
import Setting from "./setting.vue";
import { listGroups, createOne } from "@/api/FlowDefine";
import DefineApi from "@/api/FlowDefine";
import { cleanUnrequiredWidget, initBranchExp } from "@/components/flow/common/FormExp";
import { filterConditionWidgets, resetAllNodeFormAuth } from "@/components/flow/common/FormAuth";
const flowNodeRef = ref(null)
const flow = useFlowStore();
const router = useRouter();
const isEdit = computed(() => {
  return $.isNotEmpty(flow.id)
})
let launching = ref(false); // 流程发布中
let baseBox = ref(); // 基本信息组件
let flowBox = ref(); // 流程组件
let step = ref(1);
const hanldeStepClick = (nStep) => {
  if (step.value == 1) {
    baseBox.value.validate();
  } else if (step.value == 2) {
    let { nodeConfig, flowWidgets } = flow;
    cleanUnrequiredWidget(flowWidgets, nodeConfig);
  }
  step.value = nStep;
};

const back = () => {
  router.push("/flow/define");
};


const deploy = () => {
  let t = $.freeStore(flow)
  launching.value = true;
  if (isEdit.value) {
    DefineApi.updateOne({
      ...t,
      flowWidgets: JSON.stringify(t.flowWidgets),
      flowDefineType: 'EDA',
      flowDefine: JSON.stringify(t.nodeConfig)
    }).then(res => {
      launching.value = false;
      $.message.success('更新成功');
      $.navigateBack(-1)
    })
  } else {
    DefineApi.createOne({
      ...t,
      flowWidgets: JSON.stringify(t.flowWidgets),
      flowDefineType: 'EDA',
      flowDefine: JSON.stringify(t.nodeConfig)
    }).then(res => {
      launching.value = false;
      $.message.success('新建成功');
      $.navigateBack(-1)
    })
  }

};


</script>
<style lang="less" scoped>
@import "@/styles/variables.module.less";

@header-height: 70px;
@canvas-bg: @MainContentBg;

.fd-nav {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 1;
  width: 100%;
  height: @header-height;
  box-shadow: 0 1px 6px 0 rgba(0, 0, 0, 0.04);
  font-size: 14px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  user-select: none;

  .fd-nav-left {
    flex: 1;
    display: flex;
    align-items: center;
    height: 100%;

    .back {
      width: 32px;
      height: 32px;
      display: flex;
      align-items: center;
      justify-content: center;
      cursor: pointer;
      color: var(--color-text-2);
      background-color: var(--color-secondary);
      font-size: 14px;
      border-radius: var(--border-radius-small);

      &:hover {
        background-color: var(--color-secondary-hover);
      }
    }

    .name-desc {
      display: flex;
      flex-direction: column;
      max-width: 200px;
      margin-left: 15px;

      .name {
        font-size: 16px;
        height: 24px;
        line-height: 24px;
        white-space: nowrap;
        text-overflow: ellipsis;
        overflow: hidden;
      }

      .desc {
        line-height: 20px;
        font-size: 12px;
        color: #8f959e;
        white-space: nowrap;
        text-overflow: ellipsis;
        overflow: hidden;
      }
    }
  }

  .fd-nav-mid {
    flex: 2;
  }

  .fd-nav-right {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: flex-end;
  }
}

.fd-main {
  position: fixed;
  top: @header-height;
  left: 0;
  right: 0;
  bottom: 0;
  height: calc(100% - @header-height);
  background-color: @canvas-bg;
  overflow: hidden;

  .fd-main-box {
    height: 100%;
    overflow-y: auto;
  }
}
</style>
